const router = require('koa-router')()
const upload = require('../utils/upload')
const userapi = require('../controller/user')
const questionnaireapi = require('../controller/questionnaire')
const fileapi = require('../controller/file')

router.post('/api/login', async (ctx, next) => {
  const {user, pass} = ctx.request.body
  ctx.body = await userapi.login(user, pass)
})

router.post('/api/postquestionnaire', async (ctx, next) => {
  const {type, group, unit, answers, questuser} = ctx.request.body
  ctx.body = await questionnaireapi.saveQuestionnaire({type, group, unit, answers, questuser})
})

router.get('/api/questionnaire', async (ctx, next) => {
  const {type, group, unit} = ctx.request.query
  ctx.body = await questionnaireapi.getQuestionnaire({type, group, unit})
})

router.post('/api/uploadImage', upload.single('file'), async (ctx, next) => {
  const {destination, filename, path, size, mimetype} = ctx.req.file
  ctx.body = await fileapi.getFileInfo({destination, filename, path, size, mimetype})
})

router.post('/api/getFileList', async (ctx, next) => {
  const { fileids } = ctx.request.body
  ctx.body = await fileapi.getFileList({ fileids })
})

module.exports = router
